package jbl.service.impl;

import java.util.ArrayList;
import java.util.List;

import jbl.dao.TuserGroupDaoI;
import jbl.dao.impl.TuserGroupDaoImpl;
import jbl.model.Code_tusergroup;
import jbl.pageModel.DataGrid;
import jbl.pageModel.UserGroup;
import jbl.service.UserGroupServiceI;
import jbl.util.Encrypt;
import jbl.util.Json;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * 用户权限菜单表
 * */
@Service("userGroupService")
public class UserGroupServiceImpl implements UserGroupServiceI{
	
	private static final Logger logger = Logger.getLogger(UserGroupServiceImpl.class);

	
	private TuserGroupDaoI userGroupDao;
	


	
	public TuserGroupDaoI getUserGroupDao() {
		return userGroupDao;
	}
	@Autowired
	public void setUserGroupDao(TuserGroupDaoImpl userGroupDao) {
		this.userGroupDao = userGroupDao;
	}


	@Override
	public DataGrid showUserGroupList() {
		DataGrid dg = new DataGrid();
		try {
			List<UserGroup> rl = new ArrayList<UserGroup>();
			String hql = "from Code_tusergroup t ";
			List<Code_tusergroup> nl = userGroupDao.find(hql);
			if(null != nl && nl.size() > 0){
				for(Code_tusergroup t : nl){
					UserGroup userGroup2 = new UserGroup();
					BeanUtils.copyProperties(t, userGroup2);
					rl.add(userGroup2);
				}
				dg.setTotal((long)rl.size());
				dg.setRows(rl);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询userGroup表错误:"+e.getMessage());
		}
		return dg;
	}
	
	@Override
	public Json addUserGroup(UserGroup userGroup){
		Json j = new Json();
		try {
			Code_tusergroup tusergroup = new Code_tusergroup();
			
			String userid = Encrypt.createUUID();
			tusergroup.setId(userid);
			tusergroup.setUserGroupName(userGroup.getUserGroupName());

			userGroupDao.save(tusergroup);
			j.setMsg("添加成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setMsg("添加失败");
		}
		return j;
	}
	
	@Override
	public Json editUserGroup(UserGroup userGroup){
		Json j = new Json();
		try {
			Code_tusergroup tusergroup = new Code_tusergroup();
			BeanUtils.copyProperties(userGroup, tusergroup);
			userGroupDao.update(tusergroup);
			j.setMsg("修改成功");
			j.setSuccess(true);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			j.setMsg("修改失败");
		}
		return j;
	}
	

	@Override
	public Json deleteUserGroup(String  ids) {
		Json j = new Json();
		try {
			if(ids != null){
				for (String id : ids.split(",")) {
					if(!id.trim().equals("")){
						Code_tusergroup t = userGroupDao.get(Code_tusergroup.class,id.trim());
						userGroupDao.delete(t);
					}
				}
			}
			j.setMsg("删除成功");
			j.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg("删除失败");
		}
		return j;
	}
	@Override
	public List<UserGroup> comboboxInfo() {
		List<UserGroup> rl = new ArrayList<UserGroup>();
		try {
			
			String hql = "from Code_tusergroup t ";
			List<Code_tusergroup> nl  = userGroupDao.find(hql);
			if(null != nl && nl.size() > 0){
				for(Code_tusergroup t : nl){
					UserGroup userGroup2 = new UserGroup();
					BeanUtils.copyProperties(t, userGroup2);
					rl.add(userGroup2);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询userGroup表错误:"+e.getMessage());
		}
		return rl;
	}

}











